//
//  LYSettingView.swift
//  1010
//
//  Created by 米明 on 15/7/25.
//  Copyright (c) 2015年 米明. All rights reserved.
//

import UIKit

class LYSettingView: UIView {
    
    var bg : UIVisualEffectView!
    var settingBtns : [UIButton]=[]
    var infoCallBack : BtnCallBack!
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        self.backgroundColor=UIColor.clear
        setUpBg()
        createSettingBtns()
        show()
    }
    
    let BTN_WIDTH = CGFloat(50)
    let BTN_SPACE = CGFloat(40)
    
    func createSettingBtns() {
        
        let images = ["v_on","sun","info","emall"];
        
        for i in 0...3 {
            let btn = produceButton(frame: CGRect(x: 0, y: 0, width: BTN_WIDTH, height: BTN_WIDTH))!
            btn.center=CGPoint(x: SCREEN_SIZE.width/2.0, y: SCREEN_SIZE.height/2.0+CGFloat(2*(i))*BTN_WIDTH)
            btn.addTarget(self, action: #selector(settingAction(_:)), for: UIControl.Event.touchUpInside)
            btn.tag=i
            btn.setImage(UIImage(named: images[i]), for: UIControl.State.normal)
            btn.transform=CGAffineTransform(scaleX: 0, y: 0)
            settingBtns.append(btn)
            
            if i==0 {
                if DataCenter.getMusicSwitch() {
                    btn.setImage(UIImage(named: "v_on"), for: UIControl.State.normal)
                }else{
                    btn.setImage(UIImage(named: "v_off"), for: UIControl.State.normal)
                }
            }else if i==1 {
                if DataCenter.getSun() {
                    btn.setImage(UIImage(named: "sun"), for: UIControl.State.normal)
                }else{
                    btn.setImage(UIImage(named: "moon"), for: UIControl.State.normal)
                }
            }
        }
    }
    
    //传递函数
    func infoActionCallBack(callBack:@escaping (_ index:Int)->()) {
        infoCallBack = callBack
    }
    
    @objc func settingAction(_ btn:UIButton) {
        if infoCallBack != nil  {
            
            switch btn.tag {
            case 0:
                if DataCenter.getMusicSwitch() {
                    DataCenter.setMusicSwitch(isOn: false)
                }else{
                    DataCenter.setMusicSwitch(isOn: true)
                }
                if DataCenter.getMusicSwitch() {
                    btn.setImage(UIImage(named: "v_on"), for: UIControl.State.normal)
                }else{
                    btn.setImage(UIImage(named: "v_off"), for: UIControl.State.normal)
                }
            case 1:
                if DataCenter.getSun() {
                    DataCenter.setSun(isSun: false)
                }else{
                    DataCenter.setSun(isSun: true)
                }
                if DataCenter.getSun() {
                    btn.setImage(UIImage(named: "sun"), for: UIControl.State.normal)
                }else{
                    btn.setImage(UIImage(named: "moon"), for: UIControl.State.normal)
                }
            default:break
            }
            
            infoCallBack(btn.tag)
        }
    }
    
    //MARK: 生产button
    func produceButton(frame: CGRect) -> UIButton! {
        let btn = UIButton(type: UIButton.ButtonType.system)
        btn.frame=frame
        btn.layer.masksToBounds=true
        btn.layer.cornerRadius=SPACE
        btn.tintColor=UIColor.white
        self.addSubview(btn)
        return btn
    }
    
    func setUpBg() {
        bg=UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.dark))
        bg?.frame=UIScreen.main.bounds
        bg.alpha=0.1
        self.addSubview(bg)
    }
    
    func show() {
        UIView.animate(withDuration: 0.3, animations: {
            self.bg.alpha=0.98
            }, completion: nil)
        
        //shareBtns
        var i = 0
        for btn in settingBtns {
            UIView.animate(withDuration: 0.3, delay: Double(i) * 0.1, options: [], animations: {
                btn.center.y -= self.BTN_WIDTH*3
                btn.transform = CGAffineTransform(scaleX: 1, y: 1)
                }, completion: { (finished) -> Void in
                    
            })
            i += 1
        }
    }
    
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss()

    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.3, animations: {
            self.bg.alpha=0
            }) { (finished) -> Void in
                self.removeFromSuperview()
        }
        //shareBtns
        var i = 0
        for btn in settingBtns {
            UIView.animate(withDuration: 0.3, delay: Double(i) * 0.1, options: [], animations: {
                btn.center.y -= 3*self.BTN_WIDTH
                }, completion: { (finished) -> Void in
                    
            })
            UIView.animate(withDuration: 0.3, delay: Double(i) * 0.1, options: [], animations: {
                btn.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
                }, completion: { (finished) -> Void in
                    
            })
            i += 1
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
